﻿using GalaSoft.MvvmLight;
using MMS.Model;
using GalaSoft.MvvmLight.Command;
using System.Windows;
using System.Windows.Controls;

namespace MMS.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;

     
        private string _title = "医疗管理系统";

        /// <summary>
        /// Gets the WelcomeTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string Title
        {
            get
            {
                return _title;
            }

            set
            {
                if (_title == value)
                {
                    return;
                }

                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        private WindowState _windowState;
        public WindowState WindowState
        {
            get { return _windowState; }
            set
            {
                if (_windowState != value)
                {
                    _windowState = value;
                    RaisePropertyChanged("WindowState");
                }
            }
        }

        public RelayCommand MinimizedCommand { get; private set; }
        public RelayCommand<Button> MaxOrNormalCommand { get; private set; }


        public MainViewModel(IDataService dataService)
        {
            //_dataService = dataService;
            //_dataService.GetData(
            //    (item, error) =>
            //    {
            //        if (error != null)
            //        {
            //            // Report error here
            //            return;
            //        }

            //        WelcomeTitle = item.Title;
            //    });

            BindingCommands();

        }

        private void BindingCommands()
        {
            MinimizedCommand = new RelayCommand(() => { WindowState = System.Windows.WindowState.Minimized; });
            MaxOrNormalCommand = new RelayCommand<Button>((btn) => MaxOrNoramlCommandMethod(btn));
        }


        private void MaxOrNoramlCommandMethod(Button sender)
        {
            var container = sender.Content as Grid;
            if (container != null)
            {
                var v1 = container.FindName("maxIcon") as UIElement;
                var v2 = container.FindName("normalIcon") as UIElement;

                if (this.WindowState == System.Windows.WindowState.Normal)
                {
                    if (v1 != null)
                        v1.Opacity = 1;
                    if (v2 != null)
                        v2.Opacity = 0;

                    this.WindowState = System.Windows.WindowState.Maximized;
                }
                else
                {
                    if (v1 != null)
                        v1.Opacity = 0;
                    if (v2 != null)
                        v2.Opacity = 1;

                    this.WindowState = System.Windows.WindowState.Normal;
                }
            }          
        }
        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}